﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Animation;
using FarseerPhysics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UtilityLibrary;
using Microsoft.Xna.Framework.Input;

namespace MapLibrary.Players.Skills
{
    class LaserInstance : Sprite
    {
        private Texture2D instanceImage;
        public Laser laser { get; set; }
        public int TimeToLive { get; set; }
        private float length;
        public float maxLength { get; set; }
        private Vector2 direction;
        private Rectangle source;
        public Boolean hitSurface;

        public LaserInstance() : base()
        {
            MAX_VELOCITY_X = ConvertUnits.ToSimUnits(1000);
            MAX_VELOCITY_Y = ConvertUnits.ToSimUnits(1000);
            this.Controller.MaxLinearVelocityX = MAX_VELOCITY_X;
            this.Controller.MaxLinearVelocityY = MAX_VELOCITY_Y;

            length = 0;
            hitSurface = false;

            source = new Rectangle(0, 0, 0, 5);
        }

        public override void Update()
        {
            base.Update();
            if (laser == null)
                return;
            TimeToLive--;
            if (TimeToLive < 0)//|| (hitSurface && length < 50))
            {
                laser.Deactivate();
                Remove();
            }
            /*else if (hitSurface)
            {
                //Body.LinearVelocity = Vector2.Zero;
                laser.Deactivate();
                Remove();
            }*/
        }

        public void Remove()
        {
            World.CurrentLevel.AddBodyToRemove(this);
        }

        public override bool MyOnCollision(FarseerPhysics.Dynamics.Fixture f1, FarseerPhysics.Dynamics.Fixture f2, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            Sprite s1 = f1.Body.UserData as Sprite;
            Player p2 = f2.Body.UserData as Player;
            if (p2 != null)
                return false;

            Sprite s2 = f2.Body.UserData as Sprite;
            if (s2 != null)
            {
                laser.Deactivate();
                Remove();
                return true;
            }
            else
            {
                LevelCollisionRegion lcr = f2.Body.UserData as LevelCollisionRegion;
                if (lcr != null)
                {
                    laser.hitSurface = true;
                    hitSurface = true;
                    return true;
                }
                return false; //if !sprite && !levelcollisionregion
            }
        }

        public override void Draw(SpriteBatch batch, WorldViewport viewport)
        {

            if (instanceImage == null)
                instanceImage = World.ContentManager.Load<Texture2D>(@"SpriteTextures/Laser/Laser");

            Vector2 playerPos = World.CurrentLevel.Player.GetPosition();
            length = (float)(laser.portionOfChunk() * maxLength);
            Vector2 laserPos = new Vector2(playerPos.X+length/4, playerPos.Y);
            SetPosition(laserPos);

            direction = new Vector2((float)Mouse.GetState().X, (float)Mouse.GetState().Y);
            float angle = (float)Math.Atan2(direction.Y-laserPos.Y,direction.X-laserPos.X);
            /*float x0 = (length * (float)Math.Cos(angle)) + laserPos.X;
            float y0 = (length * (float)Math.Sin(angle)) + laserPos.Y;
            float x2 = (instanceImage.Height * (float)Math.Sin(angle)) + x0;
            float y2 = (instanceImage.Height * (float)Math.Cos(angle)) + y0;
            float x1 = x2 - (length * (float)Math.Cos(angle));
            float y1 = y2 - (length * (float)Math.Sin(angle));*/
            

            //SetRotation(angle);
            
            Scale = new Vector2((length/maxLength), 1);

            batch.Draw(
                CurrentTexture,
                viewport.WorldToViewportCoords(GetPosition()),
                null,
                Color.White,
                GetRotation(),
                GetCenter(),
                Scale,
                SpriteEffects.None, 0);


            //Vector2 pos = new Vector2(playerPos.X+length, playerPos.Y);
            //pos /= 2.0f;

            //float angle = (float)Math.Atan2(direction.Y, direction.X);

            //source.Width = (int)length;
            //if (source.Width > maxLength)
            //    source.Width = (int)maxLength;

            /*batch.Draw(
                instanceImage,
                viewport.WorldToViewportCoords(pos),
                source,
                Color.White,
                angle,
                new Vector2(source.Center.X, source.Center.Y),
                Scale,
                SpriteEffects.None, 0);*/
        }
    }
}
